setwd('/Users/qinkevin/PycharmProjects/An-introduction-to-analysis-of-financial-data-with-R')
da=read.table('m-intcsp7309.txt',header = T)
intc=log(da$intc+1)
rtn=ts(intc,frequency = 12,start = c(1973,1))
plot(rtn,type='l',xlab='year',ylab='ln-rtn')
t.test(intc)
Box.test(intc,lag=12,type='Ljung')
par(mfcol=c(2,1))
acf(intc,lag=24)
acf(abs(intc),lag=24)
Box.test(abs(intc),lag=12,type='Ljung')
y=intc-mean(intc)
Box.test(y^2,lag=12,type='Ljung')
source('archTest.R')
archTest(y,12)
fx=read.table('d-useu9910.txt',header = T)
fxeu=log(fx$rate)
eu=diff(fxeu)
Box.test(eu,lag=20,type='Ljung')
t.test(eu)
Box.test(eu^2,lag=20,type='Ljung')
source('archTest.R')
archTest(eu,20)
library(fGarch)
da=read.table('m-intcsp7309.txt',header = T)
intc=log(da$intc+1)
m1=garchFit(~1+garch(3,0),data=intc,trace=F)
m2=garchFit(~1+garch(1,0),data=intc,trace = F)
resi=residuals(m2,standardize=T)
tdx=c(1:444)/12+1973
par(mfcol=c(3,1))
plot(tdx,resi,xlab='year',ylab='stand-resi',type='l')
acf(resi,lag=20)
pacf(resi^2,lag=20)
plot(m2)
m3=garchFit(~1+garch(1,0),data=intc,trace=F,cond.dist = 'std')
mm1=garchFit(~1+garch(11,0),data=eu,trace=F)
m4=garchFit(~1+garch(1,1),data=intc,trace = F)
v1=volatility(m4)
resi=residuals(m4,standardize=T)
vol=ts(v1,frequency = 12,start = c(1973,1))
res=ts(resi,frequency = 12,start = c(1973,1))
par(mfcol=c(2,1))
plot(vol,xlab='year',ylab='volatility',type='l')
plot(res,xlab='year',ylab='st. resi',type='l')
par(mfcol=c(2,2))
acf(resi,lag=24)
pacf(resi,lag=24)
acf(resi^2,lag=24)
pacf(resi^2,lag=24)
par(mfcol=c(1,1))
upp=0.0113+2*v1
low=0.0113-2*v1
tdx=c(1:444)/12+1973
plot(tdx,intc,xlab='year',ylab='series',type='l',ylim=c(-0.6,0.6))
lines(tdx,upp,lty=2,col='red')
lines(tdx,low,lty=2,col='red')
abline(h=c(0.0113))
m5=garchFit(~1+garch(1,1),data=intc,trace=F,cond.dist = 'std')
v2=volatility(m5)
m6=garchFit(~1+garch(1,1),data=intc,trace = F,cond.dist = 'sstd')
v3=volatility(m6)
par(mfcol=c(3,1))
plot(tdx,v1,xlab='year',ylab='volatility',type='l',ylim=c(0.06,0.3))
title(main='(a) Gaussian ')
plot(tdx,v2,xlab='year',ylab='volatility',type='l',ylim=c(0.06,0.3))
title(main='(b) Student-t')
plot(tdx,v3,xlab='year',ylab='volatility',tupe='l',ylim=c(0.06,0.3))
title(main='(c) Skew Student-t')
cor(cbind(v1,v2,v3))
library(fBasics)
basicStats(intc)
tt=-0.5526/sqrt(6/444)
tt=(0.8717-1)/0.0629
pv=2*pnorm(tt)
yt=intc-mean(intc)
m1=arima(yt^2,order=c(1,0,1))
mean(intc)
fit=yt^2-m1$residuals
v3=volatility(m6)
cor(v3,sqrt(fit))
source('Igarch.R')
mm=Igarch(intc)
y=intc*100
source('garchM.R')
garchM(y)
sp5=scan(file = 'sp500.txt')
source('Egarch.R')
da=read.table('m-ibmsp6709.txt',header = T)
dim(da)
ibm=log(da$ibm+1)
Box.test(ibm,lag=12,type='Ljung')
m1=Egarch(ibm)
stresi=m1$residuals/m1$volatility
tdx=c(1:516)/12+1967
par(mfcol=c(2,1))
plot(tdx,ibm,xlab='year',ylab='logrtn',type='l')
plot(tdx,stresi,xlab='year',ylab='stresi',type='l')
Box.test(stresi,lag=10,type='Ljung')
Box.test(stresi,lag=20,type='Ljung')
Box.test(stresi^2,lag=10,type='Ljung')
Box.test(stresi^2,lag=20,type='Ljung')
da=read.table('d-useu9910.txt',header = T)
fx=log(da$rate)
eu=diff(fx)*100
source('Tgarch11.R')
m1=Tgarch11(eu)
at=m1$residuals
sigt=m1$volatility
resi=at/sigt
Box.test(resi,lag=10,type='Ljung')
Box.test(resi,lag=20,type='Ljung')
Box.test(resi^2,lag=20,type='Ljung')
m1=garchFit(~1+aparch(1,1),data=eu,trace=F)
m2=garchFit(~1+aparch(1,1),data=eu,delta = 2,include.delta = F,trace = F)
da=read.table('d-useu9910.txt',header = T)
fx=log(da$rate)
eu=diff(fx)*100
source('Ngarch.R')
m1=Ngarch(eu)
res=m1$residuals
vol=m1$volatility
resi=res/vol
Box.test(resi,lag=10,type='Ljung')
Box.test(resi^2,lag=10,type='Ljung')
da=read.table('d-sp58010.txt',header = T)
x=da[,c(1:3,9)]
dim(x)
source('vold2m.R')
m1=vold2m(x)
v1=m1$volatility
cnt=m1$ndays
cnt[1:5]
m2=vold2m(x,ma=1)
v2=m2$volatility
da1=read.table('m-sp56710.txt',header = T)
sp=log(da1[,9])
sp5=diff(sp)
library(fGarch)
m3=garchFit(~1+garch(1,1),data=sp5,trace = F)
v3=volatility(m3)
v3=v3[158:524]
v1=ts(v1,frequency = 12,start = c(1980,1))
v2=ts(v2,frequency = 12,start = c(1980,1))
v3=ts(v3,frequency = 12,start=c(1980,1))
max(v1,v2,v3)
par(mfcol=c(3,1))
plot(v1,xlab='year',ylab='vol',type='l',ylim=c(0,.3))
title(main='(a) No correlations')
plot(v2,xlab='year',ylab='vol',type='l',ylim=c(0,.3))
title(main='(b) Lag-1 correlation')
plot(v3,xlab='year',ylab='vol',type='l',ylim=c(0,.3))
title(main='(c) Garch(1,1)')

